package com.ext.portlet.search.action;

import java.util.List;

import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.ext.portlet.common.MBConstants;
import com.ext.portlet.registration.model.Parent;
import com.ext.portlet.registration.model.School;
import com.ext.portlet.registration.model.Student;
import com.ext.portlet.registration.model.Teacher;
import com.ext.portlet.registration.service.ParentLocalServiceUtil;
import com.ext.portlet.registration.service.SchoolLocalServiceUtil;
import com.ext.portlet.registration.service.StudentLocalServiceUtil;
import com.ext.portlet.registration.service.TeacherLocalServiceUtil;
import com.liferay.portal.dao.orm.hibernate.DynamicQueryImpl;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.struts.PortletAction;

public class SearchAction extends PortletAction {

	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
				
		String cmd = ParamUtil.getString(req, Constants.CMD);
		System.out.println("cmd>>>>>>>"+cmd);
		int menubar = ParamUtil.getInteger(req, "search");
		System.out.println("menubar>>>>>"+menubar);
		
		if(cmd.equals("search_menu")){
			
			switch(menubar){
			
			case 1 :{
						req.setAttribute("options", "Institution");
						break;
					}
			case 2 :{
						req.setAttribute("options", "Board");
						break;
					}
			case 3 :{
						req.setAttribute("options", "Student");
						break;
					}
			case 4 :{
						req.setAttribute("options", "Teacher");
						break;
					}
			case 5 :{
						req.setAttribute("options", "Parent");
						break;
					}
		    default:{
					req.setAttribute("options", "Institution");
					}
			}
					
		}else if(cmd.equals("search")){
			String hidd_val = ParamUtil.getString(req, "hidd_val");
			String name = ParamUtil.getString(req, "name");
			String countryId = ParamUtil.getString(req, "countryId");
			String regionId = ParamUtil.getString(req, "regionId");			
			String cityId = ParamUtil.getString(req, "cityId");
			
			if(Validator.isNotNull(name) && hidd_val.equals("Institution")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(School.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("name", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<School> schools = (List)SchoolLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] groupIds = new long[schools.size()];
                
                for(int i=0; i<schools.size(); i++){
                	School school = schools.get(i);
                	groupIds[i]= school.getGroupId();	                	
                }     
				
				System.out.println("School List>>>>>>"+groupIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", groupIds);
				
			} else if(hidd_val.equals("Student")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(Student.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("firstName", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<Student> students = (List)StudentLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] studentIds = new long[students.size()];
                
                for(int i=0; i<students.size(); i++){
                	Student student = students.get(i);
                	studentIds[i]= student.getStudentId();	                	
                }     
				
				
				System.out.println("School List>>>>>>"+studentIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", studentIds);
				
			} else if(hidd_val.equals("Teacher")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(Teacher.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("firstName", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<Teacher> teachers = (List)TeacherLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] teacherIds = new long[teachers.size()];
                
                for(int i=0; i<teachers.size(); i++){
                	Teacher teacher = teachers.get(i);
                	teacherIds[i]= teacher.getTeacherId();	                	
                }     
				
				
				System.out.println("Teacher List>>>>>>"+teacherIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", teacherIds);
				
			} else if(hidd_val.equals("Parent")){
				
				DetachedCriteria dCriteria = DetachedCriteria.forClass(Parent.class);		            
	            if(Validator.isNotNull(name))
	            	dCriteria.add(Restrictions.like("firstName", name+"%"));
	             
                dCriteria.add(Restrictions.eq("status", MBConstants.reg_active));
                if(Validator.isNotNull(countryId))
                	dCriteria.add(Restrictions.eq("country", countryId));
                if(Validator.isNotNull(regionId))
                	dCriteria.add(Restrictions.eq("state", regionId));
                if(Validator.isNotNull(cityId))
                	dCriteria.add(Restrictions.eq("city", cityId));	             
                
                DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

                List<Parent> parents = (List)ParentLocalServiceUtil.getService().dynamicQuery(dynamicQuery);
                long[] parentIds = new long[parents.size()];
                
                for(int i=0; i<parents.size(); i++){
                	Parent parent = parents.get(i);
                	parentIds[i]= parent.getParentId();	                	
                }   
                
                System.out.println("Teacher List>>>>>>"+parentIds.length);
				
				req.setAttribute("hidd_val", hidd_val);
				req.setAttribute("Ids", parentIds);
			}
			
			req.setAttribute("PAGE", "/html/portlet/ext/search/search_result.jsp");
			return mapping.findForward("portlet.ext.search.view");
			
		}
			
		req.setAttribute("PAGE", "/html/portlet/ext/search/search_result.jsp");
		return mapping.findForward("portlet.ext.search.view");
	}
	
}
